var JS_JOB = function () {
    var owner = this;
    owner.dom_tag = "job";
    owner.dom_edit = $("#" + owner.dom_tag + "_div_add_edit");
    owner.dom_table = $("#" + owner.dom_tag + "_table");
    owner.currentId = null;
    owner.map_data = {};
    owner.sortName = "date_update";
    owner.sortOrder = "desc";
    owner.listBoxDep1 = new Array();
    owner.listBoxDep2 = new Array();

    owner.dom_table.datagrid({
        onSortColumn: function (sort, order) {
            owner.sortName = sort;
            owner.sortOrder = order;
            owner.getPage(1);
        }
    }).datagrid("getPager").pagination({
        layout: ["list", "sep", "first", "prev", "sep", "manual", "sep", "next", "last", "refresh", "sep"],
        pageList: [10, 20, 50, 100],
        pageSize: 20,
        buttons: [{
            iconCls: "icon-add",
            handler: function () {
                owner.currentId = null;
                owner.dom_edit.window({
                    "title": "添加"
                }).window("open").window("center");
                owner.resetSaveOrEdit();
            }
        }, {
            iconCls: "icon-edit",
            handler: function () {
                var row = owner.dom_table.datagrid("getSelected");
                if (row == null) {
                    $.messager.alert("提示信息", "必须选择一条记录！");
                    return;
                }
                owner.currentId = row.itemId;
                owner.dom_edit.window({
                    "title": "编辑"
                }).window("open").window("center");
                owner.resetSaveOrEdit();
            }
        }, {
            iconCls: "icon-remove",
            handler: function () {
                var row = owner.dom_table.datagrid("getSelected");
                if (row == null) {
                    $.messager.alert("提示信息", "必须选择一条记录！");
                    return;
                }
                $.messager.confirm("提示信息", "确定删除？", function (isDel) {
                    if (isDel) {
                        var id = row.itemId;
                        $.ajax({
                            type: "post",
                            url: "/job/delete",
                            cache: false,
                            dataType: "json",
                            data: {
                                "id": id
                            }
                        }).done(function (result) {
                            if (result.success) {
                                owner.dom_table.datagrid("deleteRow", owner.dom_table.datagrid("getRowIndex", row));
                            } else {
                                $.messager.alert("提示信息", result.message);
                            }
                        }).fail(function () {
                            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
                        });
                    }
                });
            }
        }],
        onSelectPage: function (pageNum, pageSize) {
            owner.getPage(pageNum, pageSize);
        }
    });

    $("#" + owner.dom_tag + "_search_dep1").combobox({
        onSelect: function (record) {
            owner.loadDep2(record.value, $("#" + owner.dom_tag + "_search_dep2"));
        }
    });

    $("#" + owner.dom_tag + "_ipt_p2").combobox({
        onSelect: function (record) {
            owner.loadDep2(record.value, $("#" + owner.dom_tag + "_ipt_p3"));
        }
    });

    owner.init = function () {
        $("#" + owner.dom_tag + "_search_name").textbox();
        $("#" + owner.dom_tag + "_search_dep1").combobox();
        $("#" + owner.dom_tag + "_search_dep2").combobox();
        owner.loadDep1();
        owner.getPage(1);
    };

    owner.loadDep1 = function () {
        $.ajax({
            type: "get",
            url: "/dep/getForList",
            cache: false,
            dataType: "json",
            data: {
                level: "1"
            }
        }).done(function (result) {
            if (result.success) {
                for (var key in result.data) {
                    owner.listBoxDep1.push({
                        value: key,
                        text: result.data[key]
                    });
                }
                $("#" + owner.dom_tag + "_search_dep1").combobox("loadData", owner.listBoxDep1);
                $("#" + owner.dom_tag + "_ipt_p2").combobox("loadData", owner.listBoxDep1);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.loadDep2 = function (father, dom) {
        $.ajax({
            type: "get",
            url: "/dep/getForList",
            cache: false,
            dataType: "json",
            data: {
                level: "2",
                father: father
            }
        }).done(function (result) {
            if (result.success) {
                owner.listBoxDep2 = new Array();
                for (var key in result.data) {
                    owner.listBoxDep2.push({
                        value: key,
                        text: result.data[key]
                    });
                }
                dom.combobox("loadData", owner.listBoxDep2);
            } else {
                $.messager.alert("提示信息", result.message);
            }
        });
    };

    owner.resetSaveOrEdit = function () {
        if (owner.currentId == null) {
            owner.dom_edit.find(".easyui-textbox").textbox("clear")
                .end().find(".easyui-combobox").combobox("clear")
                .end().find(".textbox").val("");
        } else {
            var item = owner.map_data[owner.currentId];
            $("#" + owner.dom_tag + "_ipt_p1").textbox("setValue", item.name);
            $("#" + owner.dom_tag + "_ipt_p2").combobox("setValue", item.dep1);
            $("#" + owner.dom_tag + "_ipt_p3").combobox("setValue", item.dep2);
            $("#" + owner.dom_tag + "_ipt_p4").combobox("setValue", item.jd);
        }
    };

    owner.saveOrEdit = function (dom) {
        dom.linkbutton("disable");
        var isAllValid = new Array();
        owner.dom_edit.find(".easyui-textbox").each(function () {
            if (!$(this).textbox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        owner.dom_edit.find(".easyui-combobox").each(function () {
            if (!$(this).combobox("isValid")) {
                isAllValid.push($(this).prev("label").html());
            }
        });
        if (isAllValid.length > 0) {
            $.messager.alert("提示信息", isAllValid.join(",") + " 输入不正确！");
            dom.linkbutton("enable");
            return;
        }
        $.ajax({
            type: "post",
            url: "/job/addOrEdit",
            cache: false,
            dataType: "json",
            data: {
                "id": owner.currentId,
                "name": $("#" + owner.dom_tag + "_ipt_p1").textbox("getValue"),
                "dep1": $("#" + owner.dom_tag + "_ipt_p2").combobox("getValue"),
                "dep2": $("#" + owner.dom_tag + "_ipt_p3").combobox("getValue"),
                "jd": $("#" + owner.dom_tag + "_ipt_p4").textbox("getValue")
            }
        }).done(function (result) {
            if (result.success) {
                var item = result.data;
                if (owner.currentId == null) {
                    owner.dom_table.datagrid("appendRow", owner.formatItem(item));
                } else {
                    owner.dom_table.datagrid("updateRow", {
                        index: owner.currentRow,
                        row: owner.formatItem(item)
                    });
                }
                owner.map_data[item.id] = item;
                owner.dom_edit.window("close");
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        }).complete(function () {
            dom.linkbutton("enable");
        });
    };

    owner.formatItem = function (obj) {
        var item = {
            "itemId": obj.id,
            "p1": obj.name,
            "p2": obj.dep1Name,
            "p3": obj.dep2Name,
            "p4": obj.creator,
            "p5": obj.jd,
            "date_create": obj.dateCreate.date(2),
            "date_update": obj.dateUpdate.date(2)
        };
        return item;
    };

    owner.getPage = function (pageNum, pageSize) {
        if (pageSize == null) {
            pageSize = owner.dom_table.datagrid("getPager").pagination("options").pageSize;
        }
        var params = {
            "name": $("#" + owner.dom_tag + "_search_name").textbox("getValue"),
            "dep1": $("#" + owner.dom_tag + "_search_dep1").combobox("getValue"),
            "dep2": $("#" + owner.dom_tag + "_search_dep2").combobox("getValue"),
            "pageNum": pageNum,
            "pageSize": pageSize,
            "sort": owner.sortName+" "+owner.sortOrder
        };
        $.ajax({
            type: "get",
            url: "/job/search",
            cache: false,
            dataType: "json",
            data: params
        }).done(function (result) {
            if (result.success) {
                var ps = result.data;
                var data = new Array();
                owner.map_data = {};
                if (ps.size == 0) {
                    $.messager.alert("提示信息", "暂无数据！");
                    return;
                }
                for (var i = 0; i < ps.list.length; i++) {
                    var obj = ps.list[i];
                    owner.map_data[obj.id] = obj;
                    data.push(owner.formatItem(obj));
                }
                owner.dom_table.datagrid("loadData", {
                    "total": ps.total,
                    "rows": data
                });
            } else {
                $.messager.alert("提示信息", result.message);
            }
        }).fail(function () {
            $.messager.alert("提示信息", "网络繁忙，请稍后再试！");
        });
    };


};